package com.orzxlee.Stream05;

import com.orzxlee.Stream04.Actor;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/*
    第五节 Stream流

        Stream流的收集操作
        R collect(Collector collector)  接口

        工具类Collectors提供了方法
        public static <T> Collector toList(): 收集到List集合中
        public static <T> Collector toSet():  收集到Set集合中
        public static Collector toMap(Function keyMapper,Function valueMapper): 收集到Map集合中


 */
public class Demo {
    public static void main(String[] args) {

        ArrayList<String> manList = new ArrayList<>();
        manList.add("林青霞");
        manList.add("张曼玉");
        manList.add("王祖贤");
        manList.add("柳岩");

        //1.得到名字为3个字的流
        //Stream<String> stringStream = manList.stream().filter(s -> s.length() == 3);
        //2.将上一步操作的流 收集到List集合中并遍历
//        List<String> collect = stringStream.collect(Collectors.toList());
//        collect.stream().forEach(s -> System.out.println(s));
//        manList.stream().filter(s -> s.length() == 3).
//                collect(Collectors.toList()).stream().forEach(System.out::println);


        //创建Set集合
        Set<Integer> set = new HashSet<>();
        set.add(10);
        set.add(20);
        set.add(30);
        set.add(33);
        set.add(42);

        //得到年龄大于25的流
        //Stream<Integer> integerStream = set.stream().filter(i -> i > 25);
        //收集上一步的流,收集到Set集合中,并遍历
        //set.stream().filter(i -> i > 25).collect(Collectors.toSet()).stream().forEach(System.out::println);



        //创建字符串数组,
        String [] strArray = {"林青霞,30","张曼玉,35","王祖贤,33","柳岩,25"};

        /*
        //得到年龄大于28的流
        Stream<String> stringStream = Stream.of(strArray).filter(s -> Integer.parseInt(s.split(",")[1]) > 28);

        //收集上一步得到的流,收集Map集合中,姓名作为Key,年龄作为Value
        Map<String, Integer> collectMap = stringStream.collect(Collectors.toMap(o -> o.split(",")[0],
                s -> Integer.parseInt(s.split(",")[1])));
        //Key
        collectMap.keySet().stream().forEach(System.out::println);
        //value
        collectMap.values().stream().forEach(System.out::println);
        //entrySet  键值对
        collectMap.entrySet().stream().forEach(System.out::println);
        */

        //合并写法
        Stream.of(strArray).filter(s -> Integer.parseInt(s.split(",")[1]) > 28).
                collect(Collectors.toMap(o -> o.split(",")[0],s -> Integer.parseInt(s.split(",")[1]))).
                entrySet().stream().forEach(System.out::println);
    }
}
